/**
 * new 一个对象
 * @param context
 * @param args
 * @returns {*}
 * @private
 */

function  _new(context,...args){
    console.log(args)
    const instance = Object.create(context.prototype);

    //等同于下面
    // const instance = {}
    // instance.__proto__ = Object.prototype

    const res = context.apply(instance,args)

    return typeof res==='object'?res :instance;

}


const a = _new(Object,12,3,4)
console.log(typeof a)


/**
 * 1.新建一个对象
 * 2.将对象的__protp__ 挂载到 obj.prorotype上
 * 3.调用方法
 * 4.返回一个新对象
 * @param obj
 * @returns {*}
 * @private
 */
function __new(obj){
    const instance = Object.create(obj)
    const args = [...arguments].slice(1)

    const res = obj.apply(instance,args)
    return typeof res ==='object'?res:instance;

}
